import baseUrl from './config';
import http from '@/utils/http';
import axios from 'axios';
import xxtea from '@/utils/xxtea';
const key = 'wI0mcEOZwBwC1wZh';

export const upload_url = `${baseUrl.FILE}/api/file/upload`;
export const upload_video_url = `${baseUrl.FILE}/api/admin/file/video/upload`;
export const upload_image_url = `${baseUrl.FILE}/api/admin/file/image/upload`;
export function get_file_info(uuid) {
  return http({
    url: `${baseUrl.FILE}/api/file/${uuid}`,
    method: 'GET'
  });
}

export function upload_url_post(data) {
  return http({
    url: `${baseUrl.FILE}/api/file/upload`,
    method: 'POST',
    data: data
  });
}

export function remove_file(uuid) {
  return http({
    url: `${baseUrl.FILE}/api/admin/file/remove/${uuid}`,
    method: 'GET'
  });
}

export async function upload_video(data) {
  return await http({
    url: `${baseUrl.FILE}/api/admin/file/video/upload`,
    method: 'POST',
    data: data
  });
}

export async function decryptByXXtea(t) {
  if (t) {
    const res = await axios({
      url: `${baseUrl.IMAGEPREFIX}${t}`,
      method: 'GET',
      responseType: 'arraybuffer'
    });
    const encryptData = new Int8Array(res.data);
    const decrypted = xxtea.decrypt(encryptData, key);
    const str = arrayBufferToBlob(decrypted); // 转换字符串
    return str;
  }
}

function arrayBufferToBlob(buffer) {
  var bytes = new Uint8Array(buffer);
  const blob = new Blob([bytes]);
  var str = window.URL.createObjectURL(blob);

  return str;
}
